package com.wanma.recuit.bosscontroller;


import com.wanma.recuit.pojo.Company;
import com.wanma.recuit.pojo.CompanyUser;
import com.wanma.recuit.pojo.User;
import com.wanma.recuit.service.LoginRegistService;
import com.wanma.recuit.util.CookiesUtils;
import com.wanma.recuit.util.EncryptUtils;
import com.wanma.recuit.util.MD5Utils;
import com.wanma.recuit.util.PhoneCode;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class LoginRegistController {
    @Resource
    private LoginRegistService loginRegistService;


    //跳转到登陆了页面
    @GetMapping("/bossmanage/login")
    public void Login(){
    }
    //公司登陆
    @PostMapping("/bossmanage/login")
    @ResponseBody
    public String login(CompanyUser companyUser,String code,HttpServletRequest request){
        HttpSession session = request.getSession();
        //对公司用户的密码进行2次加密
        companyUser.setUserPass(MD5Utils.md5(MD5Utils.md5(companyUser.getUserPass())));
//        //对用户手机号进行加密
//        companyUser.setUserPhone(EncryptUtils.encrypt(companyUser.getUserPhone()));
        //检测用户是否注册过
        if(loginRegistService.findUserPhone(companyUser.getUserPhone())==null)
        {
            return "noPhone";
        }
//        //如果session中没有验证码的时候加上
//        if(session.getAttribute("code")==null){
//            return "getCode";
//        }else{
//            //有验证码，判定验证码有没有失效，
//            long start = (long)session.getAttribute("start");
//            long end = System.currentTimeMillis();
//            long t =end-start;
//            if(t>=0&&t<=60000) {
//            }
//            else {
//                //验证码时间过了一分钟哦，去掉验证码
//                session.removeAttribute("code");
//                return "getCode";
//            }
//        }
//        //检查验证码是否正确
//        if(session.getAttribute("code").equals(code)){
//        }else{
//            return "differentCode";
//        }
        //检测手机号和密码是否正确
        companyUser = loginRegistService.loginCompanyUser(companyUser);
        if(companyUser!=null){
            session.setAttribute("companyUser",companyUser);
            //登录成功验证码失效 时间失效
            session.removeAttribute("code");
            session.removeAttribute("start");
        }else{
            return "error";
        }

        //查询看看有没有公司的id，来确定是否跳转到第一页
        if (companyUser.getCompanyId()==null){
            System.out.println("公司的id没有");
            return "/bossmanage/addcompanyinfo1";
        }
        //查看公司的审核状态看是否直接到首页
        //去查看公司的审核是否通过以及上次操作到第几页等等，查出所有信息

        Company company = loginRegistService.getCompany(companyUser);
        session.setAttribute("company",company);
        System.out.println(company);
        //跳转到首页
        if(company.getCheck()==true){

            return "success";
        }

        //跳转到第二页
        if(company.getTwocheckinfo()==false){
            System.out.println("跳到第二页");
            return "/bossmanage/addcompanyinfo2";
        }
        if(company.getWaitcheck()==false){
            return "/bossmanage/checkcompanyinfo";
        }
        if(company.getWaitcheck()==true){
            return "/bossmanage/fail";
        }



        return "success";
    }
    //跳转到注册页面
    @GetMapping("/bossmanage/regist")
    public void Regist(){}
    //得到手机验证码 并且存放到session
    @GetMapping("/bossmanage/getCode")
    public String getCode(String userPhone, HttpServletRequest request){
        String code = PhoneCode.getPhonemsg(userPhone);
        //得到系统的当前时间并转化为毫秒数
        long start = System.currentTimeMillis();
        HttpSession session = request.getSession();
        session.setAttribute("code",code);
        session.setAttribute("start",start);
        return code;
    }

    //公司注册
    @PostMapping("/bossmanage/regist")
    @ResponseBody
    public String regist(CompanyUser companyUser,String code,HttpServletRequest request,HttpServletResponse response){
        HttpSession session = request.getSession();
        //查看手机号码是否注册
        if(loginRegistService.findUserPhone(companyUser.getUserPhone())!=null)
        {
            return "hasPhone";
        }
        //如果session中没有验证码的时候加上
        if(session.getAttribute("code")==null){
            return "getCode";
        }else{
           //有验证码，判定验证码有没有失效，
            long start = (long)session.getAttribute("start");
            long end = System.currentTimeMillis();
            long t =end-start;
            if(t>=0&&t<=60000) {
            }
            else {
                //验证码时间过了一分钟哦，去掉验证码
                session.removeAttribute("code");
                return "getCode";
            }
        }
        //检查验证码是否正确
        if(session.getAttribute("code").equals(code)){
        }else{
            System.out.println(code);
            System.out.println(session.getAttribute("code"));
            return "differentCode";
        }
        //对密码加密两次
        companyUser.setUserPass(MD5Utils.md5(MD5Utils.md5(companyUser.getUserPass())));

        loginRegistService.registCompanyUser(companyUser);
        //注册成功验证码失效 时间失效
        session.removeAttribute("code");
        session.removeAttribute("start");

        session.setAttribute("companyUser",companyUser);
        return "success";
    }

    //应聘者注册
    @PostMapping("/manage/regist")
    @ResponseBody
    public String regist(String userPhone,String userPass,String code, HttpServletRequest request, HttpServletResponse response){

        HttpSession session = request.getSession();

        //查看手机号码是否注册
        if(loginRegistService.findPhone(userPhone)!=null)
        {
            return "hasPhone";
        }
        //如果session中没有验证码的时候加上
        if(session.getAttribute("code")==null){
            return "getCode";
        }else{
            //有验证码，判定验证码有没有失效，
            long start = (long)session.getAttribute("start");
            long end = System.currentTimeMillis();
            long t =end-start;
            if(t>=0&&t<=60000) {
            }
            else {
                //验证码时间过了一分钟哦，去掉验证码
                session.removeAttribute("code");
                return "getCode";
            }
        }
        //检查验证码是否正确
        if(session.getAttribute("code").equals(code)){
        }else{
            return "differentCode";
        }
        User user = new User();
        //对密码加密两次
        user.setPass(MD5Utils.md5(MD5Utils.md5(userPass)));
//        //对用户手机号进行加密
//        user.setPhone(EncryptUtils.encrypt(userPhone));
        user.setPhone(userPhone);
        loginRegistService.registUser(user);
        //注册成功验证码失效 时间失效
        session.removeAttribute("code");
        session.removeAttribute("start");

        session.setAttribute("user",user);
        //得到来之前的路径
        String referer=request.getParameter("referer");

        if(referer==null ||referer.equals("")||referer.contains("/login")||referer.contains("/regist")){

            return "/service/index";
        }else{
        }
        System.out.println(referer);
        return  referer;
    }
    //应聘者登陆
    @PostMapping("/manage/login")
    @ResponseBody
    public String login(String userPhone,String userPass,String code,String rememberme, HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        User user = new User();
        //对密码加密两次
        user.setPass(MD5Utils.md5(MD5Utils.md5(userPass)));
        user.setPhone(userPhone);
        //检测用户是否注册过
        System.out.println(user.getPhone());
        System.out.println(loginRegistService.findPhone(user.getPhone()));
        if(loginRegistService.findPhone(user.getPhone())==null)
        {
            System.out.println("22222222222222");
            return "noPhone";
        }
        //如果session中没有验证码的时候加上
        if(session.getAttribute("code")==null){
            return "getCode";
        }else{
            //有验证码，判定验证码有没有失效，
            long start = (long)session.getAttribute("start");
            long end = System.currentTimeMillis();
            long t =end-start;
            if(t>=0&&t<=60000) {
            }
            else {
                //验证码时间过了一分钟哦，去掉验证码
                session.removeAttribute("code");
                return "getCode";
            }
        }
        //检查验证码是否正确
        if(session.getAttribute("code").equals(code)){
        }else{
            return "differentCode";
        }
        //检测手机号和密码是否正确
        user = loginRegistService.loginUser(user);
        System.out.println(user);
       if(user!=null){
           session.setAttribute("user",user);
           //登录成功验证码失效 时间失效
           session.removeAttribute("code");
           session.removeAttribute("start");
           //判断是否记住我
           if(rememberme!=null){
               CookiesUtils.write("phone", EncryptUtils.encrypt(user.getPhone()),"/",60*60*24*30,response);
               CookiesUtils.write("pass", user.getPass(),"/",60*60*24*30,response);
           }

           String referer=request.getParameter("referer");
           if(StringUtils.isEmpty(referer)||referer.contains("/regist")||referer.contains("/login")){
               System.out.println("buneng ");
               return "/service/index";
           }else{
               System.out.println(referer);
               return referer;
           }
       }else{
           return "error";
        }

    }

    @GetMapping("/bossmanage/quit")
    public String quit(HttpServletRequest request){
        //使整个session失效
        request.getSession().invalidate();
//        //session中的某些失效
//        request.getSession().removeAttribute("user");
//        //删除cookie中的信息，设置时间为0即可
//        CookiesUtils.write("userPhone","","/",0,response);
//        CookiesUtils.write("userPass","","/",0,response);
        return "/bossmanage/login";
    }

}
